3 * Updater for link tracking tables after a page edit.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\MediaWikiServices
;
23 use Wikimedia\ScopedCallback
;
24 use Wikimedia\Rdbms\IDatabase
;
27 * Update object handling the cleanup of links tables after a page was deleted.
29 class LinksDeletionUpdate
extends DataUpdate
implements EnqueueableDataUpdate
{
41 * @param WikiPage $page Page we are updating
42 * @param int|null $pageId ID of the page we are updating [optional]
43 * @param string|null $timestamp TS_MW timestamp of deletion
46 function __construct( WikiPage
$page, $pageId = null, $timestamp = null ) {
47 parent
::__construct();
51 $this->pageId
= $pageId; // page ID at time of deletion
52 } elseif ( $page->exists() ) {
53 $this->pageId
= $page->getId();
55 throw new InvalidArgumentException( "Page ID not known. Page doesn't exist?" );
58 $this->timestamp
= $timestamp ?
: wfTimestampNow();
61 public function doUpdate() {
62 $services = MediaWikiServices
::getInstance();
63 $config = $services->getMainConfig();
64 $lbFactory = $services->getDBLoadBalancerFactory();
65 $batchSize = $config->get( 'UpdateRowsPerQuery' );
67 // Page may already be deleted, so don't just getId()
70 if ( $this->ticket
) {
71 // Make sure all links update threads see the changes of each other.
72 // This handles the case when updates have to batched into several COMMITs.
73 $scopedLock = LinksUpdate
::acquirePageLock( $this->getDB(), $id );
76 $title = $this->page
->getTitle();
77 $dbw = $this->getDB(); // convenience
79 // Delete restrictions for it
80 $dbw->delete( 'page_restrictions', [ 'pr_page' => $id ], __METHOD__
);
82 // Fix category table counts
83 $cats = $dbw->selectFieldValues(
89 $catBatches = array_chunk( $cats, $batchSize );
90 foreach ( $catBatches as $catBatch ) {
91 $this->page
->updateCategoryCounts( [], $catBatch, $id );
92 if ( count( $catBatches ) > 1 ) {
93 // Only sacrifice atomicity if necessary due to size
94 $lbFactory->commitAndWaitForReplication(
95 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
100 // Refresh counts on categories that should be empty now
101 if ( $title->getNamespace() === NS_CATEGORY
) {
102 // T166757: do the update after the main job DB commit
103 DeferredUpdates
::addCallableUpdate( function () use ( $title ) {
104 $cat = Category
::newFromName( $title->getDBkey() );
105 $cat->refreshCountsIfEmpty();
109 $this->batchDeleteByPK(
111 [ 'pl_from' => $id ],
112 [ 'pl_from', 'pl_namespace', 'pl_title' ],
115 $this->batchDeleteByPK(
117 [ 'il_from' => $id ],
118 [ 'il_from', 'il_to' ],
121 $this->batchDeleteByPK(
123 [ 'cl_from' => $id ],
124 [ 'cl_from', 'cl_to' ],
127 $this->batchDeleteByPK(
129 [ 'tl_from' => $id ],
130 [ 'tl_from', 'tl_namespace', 'tl_title' ],
133 $this->batchDeleteByPK(
135 [ 'el_from' => $id ],
139 $this->batchDeleteByPK(
141 [ 'll_from' => $id ],
142 [ 'll_from', 'll_lang' ],
145 $this->batchDeleteByPK(
147 [ 'iwl_from' => $id ],
148 [ 'iwl_from', 'iwl_prefix', 'iwl_title' ],
152 // Delete any redirect entry or page props entries
153 $dbw->delete( 'redirect', [ 'rd_from' => $id ], __METHOD__
);
154 $dbw->delete( 'page_props', [ 'pp_page' => $id ], __METHOD__
);
156 // Find recentchanges entries to clean up...
157 $rcIdsForTitle = $dbw->selectFieldValues(
161 'rc_type != ' . RC_LOG
,
162 'rc_namespace' => $title->getNamespace(),
163 'rc_title' => $title->getDBkey(),
165 $dbw->addQuotes( $dbw->timestamp( $this->timestamp
) )
169 $rcIdsForPage = $dbw->selectFieldValues(
172 [ 'rc_type != ' . RC_LOG
, 'rc_cur_id' => $id ],
176 // T98706: delete by PK to avoid lock contention with RC delete log insertions
177 $rcIdBatches = array_chunk( array_merge( $rcIdsForTitle, $rcIdsForPage ), $batchSize );
178 foreach ( $rcIdBatches as $rcIdBatch ) {
179 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__
);
180 if ( count( $rcIdBatches ) > 1 ) {
181 $lbFactory->commitAndWaitForReplication(
182 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
187 // Commit and release the lock (if set)
188 ScopedCallback
::consume( $scopedLock );
191 private function batchDeleteByPK( $table, array $conds, array $pk, $bSize ) {
192 $services = MediaWikiServices
::getInstance();
193 $lbFactory = $services->getDBLoadBalancerFactory();
194 $dbw = $this->getDB(); // convenience
196 $res = $dbw->select( $table, $pk, $conds, __METHOD__
);
199 foreach ( $res as $row ) {
200 $pkDeleteConds[] = $dbw->makeList( (array)$row, LIST_AND
);
201 if ( count( $pkDeleteConds ) >= $bSize ) {
202 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
203 $lbFactory->commitAndWaitForReplication(
204 __METHOD__
, $this->ticket
, [ 'domain' => $dbw->getDomainID() ]
210 if ( $pkDeleteConds ) {
211 $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR
), __METHOD__
);
215 protected function getDB() {
217 $this->db
= wfGetDB( DB_MASTER
);
223 public function getAsJobSpecification() {
225 'wiki' => WikiMap
::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
226 'job' => new JobSpecification(
228 [ 'pageId' => $this->pageId
, 'timestamp' => $this->timestamp
],
229 [ 'removeDuplicates' => true ],
230 $this->page
->getTitle()